{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction to Quantitative Finance\n",
    "\n",
    "Copyright (c) 2019 Python Charmers Pty Ltd, Australia, <https://pythoncharmers.com>. All rights reserved.\n",
    "\n",
    "<img src=\"img/python_charmers_logo.png\" width=\"300\" alt=\"Python Charmers Logo\">\n",
    "\n",
    "Published under the Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) license. See `LICENSE.md` for details.\n",
    "\n",
    "Sponsored by Tibra Global Services, <https://tibra.com>\n",
    "\n",
    "<img src=\"img/tibra_logo.png\" width=\"300\" alt=\"Tibra Logo\">\n",
    "\n",
    "## Module 1.1: Distributions and Random Processes\n",
    "\n",
    "### 1.1.1: Overview\n",
    "\n",
    "In this section we will review random distributions and processes, identifying their use cases and how to build them with the Python programming language.\n",
    "\n",
    "This module will also cover some background material on getting installed on your system, and some other notes on how to use these notebooks.\n",
    "\n",
    "In this first set of modules we will cover the following topics:\n",
    "\n",
    "1. How to use Jupyter Notebooks, including Markdown cells and exporting to PDF\n",
    "2. How to complete exercises and check your solutions work with *pytest*\n",
    "3. Python versions, and writing compatible code in both Python 2 and Python 3\n",
    "4. The Altair library for visualisations (we will use this extensively throughout the course)\n",
    "5. Different types of data (discrete and continuous) and how to generate random variables from them.\n",
    "6. Normal distributions, their properties and testing for normality\n",
    "7. Z-scores and moments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before we get started, let's check your setup to make sure it has the necessary software installed to run the notebooks. Click the next cell, and press: <kbd>CTRL</kbd> + <kbd>Enter</kbd> to run the code in it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.11.8 | packaged by Anaconda, Inc. | (main, Feb 26 2024, 21:34:05) [MSC v.1916 64 bit (AMD64)]\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "print(sys.version)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This course is designed to work with both Python 2 and Python 3. There are some differences, but these will be pointed out as we go.\n",
    "\n",
    "In the output above, you should see the version of Python you are using. These notebooks have been tested with versions:\n",
    "\n",
    "* 2.7 and above for Python 2\n",
    "* 3.6 and above for Python 3\n",
    "\n",
    "If your versions are lower than those, please consider upgrading - your Python system may not be receiving all updates it can, and many libraries will slowly stop adding features for your version. This is true of Python 2.7 too."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Additionally, it is recommended that Anaconda Distribution or Miniconda is used in order to make use of the conda package and environment manager. The Anaconda Distribution link can be found [here](https://docs.anaconda.com/free/anaconda/install/), whilst minconda can be found [here](https://docs.anaconda.com/free/miniconda/miniconda-install/). The full Anaconda Distribution is recommended as it comes with many useful packages preinstalled."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Throughout this course, you'll be able to run the code directly in the notebook itself. I also recommend completing the exercises within the notebook as you go. That way, you can reuse some of the code in the notebook itself for your exercises. To do that, you will need to add cells. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try adding a cell now. Click this cell, so that it has an outline around this paragraph. Then click <kbd>ESC</kbd>, and then <kbd>b</kbd>. This should create a new cell below this one."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "For more keyboard shortcuts, click <kbd>ESC</kbd>, and then <kbd>h</kbd>. The command mode is activated by clicking <kbd>ESC</kbd>, and then the command itself is selected using the letter <kbd>h</kbd> (or whatever for the other commands).\n",
    "\n",
    "If you want to write notes, like these ones, click <kbd>ESC</kbd>, and then <kbd>m</kbd>. This changes the cell type to \"markdown\", letting you add notes to the notebook as we are going."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Next, let us test your python setup. Let's check you have all the libraries installed on your computer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Everything is installed!\n"
     ]
    }
   ],
   "source": [
    "import pytest\n",
    "import numpy\n",
    "import scipy\n",
    "import statsmodels\n",
    "import altair\n",
    "import vega_datasets\n",
    "import sklearn\n",
    "import tensorflow\n",
    "import matplotlib\n",
    "import quandl\n",
    "print(\"Everything is installed!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If one of those failed, you'll get an error that looks like this:\n",
    "\n",
    "<pre><span class=\"ansi-red-fg\">---------------------------------------------------------------------------</span>\n",
    "<span class=\"ansi-red-fg\">ModuleNotFoundError</span>                       Traceback (most recent call last)\n",
    "<span class=\"ansi-green-fg\">&lt;ipython-input-2-302beb3fe330&gt;</span> in <span class=\"ansi-cyan-fg\">&lt;module&gt;</span>\n",
    "<span class=\"ansi-green-intense-fg ansi-bold\">      2</span> <span class=\"ansi-green-fg\">import</span> scipy\n",
    "<span class=\"ansi-green-intense-fg ansi-bold\">      3</span> <span class=\"ansi-green-fg\">import</span> statsmodels\n",
    "<span class=\"ansi-green-fg\">----&gt; 4</span><span class=\"ansi-red-fg\"> </span><span class=\"ansi-green-fg\">import</span> altair\n",
    "<span class=\"ansi-green-intense-fg ansi-bold\">      6</span> <span class=\"ansi-green-fg\">import</span> vega_datasets\n",
    "\n",
    "<span class=\"ansi-red-fg\">ModuleNotFoundError</span>: No module named 'altair'\n",
    "\n",
    "</pre>\n",
    "\n",
    "#### Managing packages using Conda\n",
    "##### Using prepackaged environment\n",
    "To use a prepackaged environment with the necessary packages installed, run\n",
    "\n",
    "    conda env create -f environment.yml\n",
    "\n",
    "which creates a `quant_finance` environment. You can activate this environment using:\n",
    "\n",
    "    conda activate quant_finance\n",
    "\n",
    "and then re-run the Jupyter notebook/lab in this environment to use all the required packages.\n",
    "\n",
    "**Note:** this method does not install the `maxentropy` package, refer to the last part of the pip instructions below for this package.\n",
    "\n",
    "##### Installing standalone packages\n",
    "To install a standalone package, use the following command:\n",
    "\n",
    "    conda install -y LIBRARY_NAME\n",
    "    \n",
    "For instance, to get tensorflow, run:\n",
    "\n",
    "    conda install -y tensorflow\n",
    "    \n",
    "You can run this command from Jupyter Notebooks too, and if you run many versions of Python on your computer, this helps ensure you have the right environment. Just prepend a `!` and run in a cell:\n",
    "\n",
    "    !conda install -y tensorflow\n",
    "    \n",
    "For `sklearn`, the name of the library is `scikit-learn`. Therefore you would run:\n",
    "\n",
    "    !conda install -y scikit-learn\n",
    "    \n",
    "    \n",
    "For `altair`, `vega` and `vega_datasets`, you'll need to use the `conda-forge`, which is simply a different repository of libraries than normal. Instead, use:\n",
    "    \n",
    "    !conda install -c conda-forge -y vega vega_datasets altair\n",
    "\n",
    "If the cell run, completed, and gave you an execution number, you'll see the message \"Everything is installed!\" printed after the cell. If that's the case, you are free to move onto the next Notebook!\n",
    "\n",
    "#### Managing packages using pip\n",
    "If you've installed Python yourself, you probably know how to install modules for your system. If you are unsure, try: `pip install my_module`\n",
    "\n",
    "You may also find the command `pip install -r requirements.txt` useful. \n",
    "\n",
    "The `maxentropy` package in `requirements.txt` requires deprecated `sklearn` (updated to `scikit-learn`), so first run `set SKLEARN_ALLOW_DEPRECATED_SKLEARN_PACKAGE_INSTALL=True` before `pip install -r requirements.txt`. Deprecated `sklearn` can then be uninstalled with `pip uninstall sklearn`."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
